﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BookManage.Model;
using BookManage.DAL;

namespace BookManage.BLL
{
    public class BorrowAdmin
    {
        public DataTable GetReader(int rdID)
        {
            return (BorrowDAL.GetReader(rdID));
        }

        public DataTable GetBorrow(int rdID)
        {
            return (BorrowDAL.GetBorrow(rdID));
        }

        public DataTable GetBook(int bkID, string bkName)
        {
            return (BorrowDAL.GetBook(bkID, bkName));
        }
       /* public DataTable GetrdStatus(int rdID)
        {
            return (BorrowDAL.GetrdStatus(rdID));
        }

        public DataTable GetReaderType(int rdType)
        {
            return (BorrowDAL.GetReaderType(rdType));
        }*/

        public int Add(Borrow borrow)
        {
            return (BorrowDAL.Add(borrow));
        } //借书添加Borrow记录

        public int UpdateBook(Borrow borrow)
        {
            return (BorrowDAL.UpdateBook(borrow));
        }//借书更新Book表
        public int UpdateReader(Reader reader)
        {
            return (BorrowDAL.UpdateReader(reader));
        }//借书更新Reader表
        public bool BorrowBook(Reader reader,ReaderType readertype,Book book,Reader OperatorUser)
        {
            if (reader == null || readertype == null || book == null || OperatorUser == null)
                return false;
            if (reader.rdStatus!="有效"|| (readertype.CanLendQty-reader.rdBorrowQty)<=0)
                return false;
            if (book.bkStatus != "在馆")
                return false;
            reader.rdBorrowQty++;
            ReaderDAL.Update(reader);

            book.bkStatus = "借出";
            BookDAL.Update(book);

            Borrow borrow = new Borrow();
            borrow.rdID = reader.rdID;
            borrow.bkID = book.bkID;
            borrow.IdContinueTimes = 0;
            borrow.IdDateOut = DateTime.Now;
            borrow.IdDateRetPlan = DateTime.Now.AddDays(readertype.CanLendDay);
            borrow.IsHasReturn = false;
            borrow.OperatorLend = OperatorUser.rdName;
            BorrowDAL.Add(borrow);
            return true;
        }

        public static int UpdatePay(int rdID, int bkID, float IdPunishMoney)
        {
            return (BorrowDAL.UpdatePay(rdID, bkID, IdPunishMoney));
        }

        public int UpdateContinue(Borrow borrow)
        {
            return (BorrowDAL.UpdateContinue(borrow));
        } //续借更新Borrow 表

        public static int UpdateBrwByPunish(int BorrowID,float IdOverDay, float IdOverMoney)
        {
            return (BorrowDAL.UpdateBrwByPunish(BorrowID,IdOverDay, IdOverMoney));
        } //缴纳罚款后更新Borrow表

        //还书时删除借阅信息
        public static int DeleteBorrow(Borrow borrow)
        {
            return (BorrowDAL.Delete(borrow));
        }


        //还书时更新所借图书信息
        public static int UpdateBrwByLend(Borrow borrow)
        {
            return (BorrowDAL.UpdateBrwByLend(borrow));
        }
        public static int UpdateBookByLend(Borrow borrow)
        {
            return (BorrowDAL.UpdateBookByLend(borrow));
        }
        public static int UpdateReaderByLend(Borrow borrow)
        {
            return (BorrowDAL.UpdateReaderByLend(borrow));
        }
        public static Borrow GetAllBorrow(int rdID, int bkID)
        {
            return (BorrowDAL.GetObjectByID(rdID, bkID));
        }
        public static Borrow GetBorrowByBrwID(int BrrowID)
        {
            return (BorrowDAL.GetObjectByBrwID(BrrowID));
        }
        public static Borrow GetBorrowByBkID(int bkID)
        {
            return (BorrowDAL.GetObjectByBkID(bkID));
        }
        public static int UpdatePunish(int rdID, int bkID,float IdPunishMoney,DateTime IdDateRetPlan)
        {
            return (BorrowDAL.UpdatePunish(rdID, bkID, IdPunishMoney, IdDateRetPlan));
        }

        public static DataTable GetLastBrwID()
        {
            return (BorrowDAL.GetLastBrwID());
        }

        public static int UpdateBookByPay(Borrow borrow)
        {
            return (BorrowDAL.UpdateBookByPay(borrow));
        }
    }
}
